package org.testis.webautomationmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.data.WebAutomationModuleDataNode;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.webautomationmodule.ui.WebAutomationDecorator;
import org.testis.webautomationmodule.ui.WebAutomationTestStepBuilder;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.webautomationmodule.ui.UIElementNotEditableException;
import org.testis.webautomationmodule.ui.UIElementNotFoundException;
import org.testis.webautomationmodule.ui.UIElementNotVisibleException;
import org.testis.webautomationmodule.ui.UIFormFieldDecorator;
import org.testis.webautomationmodule.ui.objects.UIButton;
import java.util.ArrayList;


/**
 * A action can be used for verification of web ui objects
 * @author nurhat
 */
public class VerifyAction extends AtofModuleAction
{

    @Override
    public boolean operateRequest(AtofModuleService request)
    {
        try {
            WebAutomationModuleProviderService service =
                (WebAutomationModuleProviderService) request;
            WebAutomationModuleDataObject dataObj =
                (WebAutomationModuleDataObject) service.getDataObj();
            if (service.getDataObj() == null) {
                throw new TestStepException(
                    TestStepResultType.MISTAKE,
                    "Web Automation Verify Test failed. A Null WebAutomationDataObject recieved....");
            }
            LogSenderService.putMessageOnQueue(
                "WebAutomationVerifyAction",
                AtofLogLevel.INFO,
                "Web Automation Verify Test Step started....",
                request.getDataObj().getMessageId());

            if (service.openWindow()) {
                if (runDecorator(service,
                                 dataObj.getLoginDataList())) {
                    return runVerification(service,
                                           dataObj.getPageObjectsDataList());

                }
            }
        } catch (UIElementNotEditableException ex) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Verify Test Step failed..." + ex.toString());
        } catch (UIElementNotFoundException ex) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Verify Test Step failed..." + ex.toString());
        } catch (UIElementNotVisibleException ex) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Verify Test Step failed..." +ex.toString());
        }catch (TestStepException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new TestStepException(
                TestStepResultType.FAULT,
                " Web Automation Verify Test Step failed at WebAutomationVerifyAction..." +
                ex.toString());
        }
        return false;

    }


    private boolean runDecorator(WebAutomationModuleProviderService request,
                                 ArrayList<WebAutomationModuleDataNode> list)
    {
        WebAutomationTestStepBuilder ts =
            new WebAutomationTestStepBuilder(request.getAutomate());
        WebAutomationDecorator decorator = ts.createTestStep(list,
                                                             request.getDataObj().
                                                             getMessageId());
        if (decorator != null) {

            if (decorator.operate()) {
                return true;
            }
            else {
                LogSenderService.putMessageOnQueue(
                    "WebAutomationVerifyAction",
                    AtofLogLevel.ERROR,
                    "Web Automation Verify Test failed ...",
                    request.getDataObj().getMessageId());
            }
        }
        else {
            LogSenderService.putMessageOnQueue(
                "WebAutomationVerifyAction",
                AtofLogLevel.ERROR,
                "Web Automation Verify Test failed. Could not create object decorator...",
                request.getDataObj().getMessageId());
        }
        return false;
    }


    private boolean runVerification(WebAutomationModuleProviderService request,
                                    ArrayList<WebAutomationModuleDataNode> list)
    {

        WebAutomationTestStepBuilder ts =
            new WebAutomationTestStepBuilder(request.getAutomate());
        //remove last one if it is button
        int listSize=list.size();
        if(list.get(listSize-1).getObjectType().equals("UIButton")){
             list.remove(listSize-1);
        }
        WebAutomationDecorator decorator = ts.createTestStep(list,
                                                             request.getDataObj().
                                                             getMessageId());
        if (decorator != null) {
            WebAutomationModuleDataObject dataObj =
                (WebAutomationModuleDataObject) request.getDataObj();
            boolean result = decorator.verify();
            if (result) {
                while (decorator != null) {
                    if (decorator instanceof UIFormFieldDecorator) {
                        dataObj.getUiObjectValueList().put(((UIFormFieldDecorator) decorator).
                                                           getAbstractId(),
                                                           ((UIFormFieldDecorator) decorator).
                                                           getValue());
                    }
                    decorator = decorator.getNextUIObjectDecorator();
                }
            }

            return result;
        }
        else {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Verify test failed at WebAutomationVerifyAction...Could not create object decorator...");

        }

    }
}



